<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/13 0013
 * Time: 11:35
 */

namespace app\printer\controller;

use think\Db;
use think\facade\Request;
//耗材订单
class OrderController extends BaseController
{
    public function index()
    {

        return $this->fetch();
    }

//    得到客户信息
    public function getCustomerInfo()
    {
        $like_value = Request::get('like_value', 0);
//        $map['user_name'] = ['like', '%' , $like_value];
//        $map['unit_name'] = ['like', '%' , $like_value];
        $map = [];
        if($id = Request::get('id')){
            $map['id'] = ['eq',  $id];
            $data = Db::name('customer')
                ->where($map)
                ->find();
            return $data;
        }
        $data = Db::name('customer')
            ->where(function($query)use($like_value){
                $query->whereOr('user_name','like','%' , $like_value)
                    ->whereOr('unit_name','like','%' , $like_value)
                    ->whereOr('tel','like','%' , $like_value)
                    ->whereOr('comment','like','%' , $like_value);
            })
            ->where($map)
            ->order(['id'=>'desc','create_time'=>'desc'])
            ->select();
        return $data;

    }

    //得到数据展示到表格
    public function getData()
    {
        $page = Request::get('page', 0);
        $limit = Request::get('limit', 15);
        $map = array();
        if(session('user_name') != 'admin'){
            $map['user_id'] = ['eq', session('user_id')];
        }
        $data = Db::name('order')->page($page, $limit)->where($map)->order(['id'=>'desc','create_time'=>'desc'])->select();
        $count = Db::name('order')->where($map)->count();

        return ['code' => 0,'count'=> $count, 'data' => $data];
    }
    //添加
    public function add()
    {

        if(Request::isPost()){
            $post_data = Request::post();
            $orderSn = get_order_sn();
            $orderData = [];
            $orderData['order_id'] = $orderSn;
            $orderData['user_id'] = session('user_id');
            $orderData['by_user'] = session('user_name');
            $orderData['apply_price'] = $post_data['apply_price'];
            $orderData['price_total_all'] = $post_data['price_total_all'];
            $orderData['pay_price'] = $post_data['pay_price'];
            $orderData['customer_id'] = $post_data['customer_id'];
            $orderData['customer_address'] = $post_data['customer_address'];
            $orderData['customer_name'] = $post_data['customer_name'];
            $orderData['customer_unit'] = $post_data['customer_unit'];
            $orderData['customer_tel'] = $post_data['customer_tel'];
            $orderData['order_date'] = getDateTime();
            $orderData['comment'] = $post_data['order_comment'];
            $orderData['invoice_type'] = $post_data['invoice_type'];
            $orderData['create_time'] = getDateTime();
            $orderData['delivery_time'] = $post_data['delivery_time'];
//            dump($orderData);die;
            //下单耗材信息 库存记录
            $order_goods_data = $inventory_recode =[];

            foreach ($post_data['print_type'] as $index => $post_datum) {
                $inventory_recode[$index]['create_time'] = getDateTime();
                $inventory_recode[$index]['create_by_user_id'] = $orderData['user_id'];
                $inventory_recode[$index]['create_by_user_name'] = $orderData['by_user'];
                $inventory_recode[$index]['customer_id'] =$post_data['customer_id'];
                $inventory_recode[$index]['unit_name'] = $post_data['customer_unit'];
                $inventory_recode[$index]['unit_name_customer'] = $post_data['customer_name'];
                $inventory_recode[$index]['customer_tel'] = $post_data['customer_tel'];
                $inventory_recode[$index]['print_type'] = $post_datum;
                $inventory_recode[$index]['order_id'] = $orderSn;
            }
            foreach ($post_data['consumable_type'] as $index => $post_datum) {
                $order_goods_data[$index]['goods_type'] = $post_datum;
                $inventory_recode[$index]['consumable_type'] = $post_datum;
            }
            foreach ($post_data['page_num'] as $index => $post_datum) {
                $order_goods_data[$index]['page_num'] = $post_datum;
            }
            foreach ($post_data['price'] as $index => $post_datum) {
                $order_goods_data[$index]['goods_price'] = $post_datum;
                $inventory_recode[$index]['price'] = $post_datum;
            }
            foreach ($post_data['num'] as $index => $post_datum) {
                $order_goods_data[$index]['buy_total'] = $post_datum;
                $inventory_recode[$index]['buy_num'] = $post_datum;
            }
            foreach ($post_data['price_total'] as $index => $post_datum) {
                $order_goods_data[$index]['price_total'] = $post_datum;
            }
            foreach ($post_data['comment'] as $index => $post_datum) {
                $order_goods_data[$index]['comment'] = $post_datum;
            }
            foreach ($post_data['inventory_id'] as $index => $inventory_id) {
                //再检查一下库存
                $dec_num = $post_data['num'][$index]; //下单的数量
                $inventory_Info = Db::name('inventory')->where(['id'=>['eq', $inventory_id]])->find(); //库存数量
                if($dec_num > $inventory_Info['num']){
                    return $this->ajaxFail(-1, $inventory_Info['print_type'] . '--'. $inventory_Info['consumable_type'] . '库存不足');
                }
                $order_goods_data[$index]['inventory_id'] = $inventory_id;
                $inventory_recode[$index]['inventory_id'] = $inventory_id;
            }
            //保存下单信息
            $res = Db::name('order')->insert($orderData);
            if($res){
                $new_order_id = Db::getLastInsID();
                foreach ($post_data['print_type'] as $index => $post_datum) {
                    $order_goods_data[$index]['order_id'] = $new_order_id;
                    $order_goods_data[$index]['create_time'] = getDateTime();
                    $order_goods_data[$index]['goods_name'] = $post_datum;


                }

                $res = Db::name('order_goods')->insertAll($order_goods_data);
                if($res){
                    //减库存
                    foreach ($post_data['inventory_id'] as $index => $inventory_id) {
                        //库存减少的数量
                        $dec_num = $post_data['num'][$index];
                        $res = Db::name('inventory')->where(['id'=>['eq', $inventory_id]])->setDec('surplus_num',$dec_num);
                        if(!$res) return $this->ajaxFail(-1, '减少库存失败');
                        //记录库存的变化
                        $recode = new RecodeController();
                        $create_recode_res = $recode -> createInventoryRecode($inventory_recode);
                        if(!$create_recode_res)   return $this->ajaxFail(-1,'创建库存记录失败');
                    }
                    return $this->ajaxSuccess();
                }else{
                    return $this->ajaxFail();
                }
            }

        }
        echo  $this->fetch();
    }

    //得到订单添加耗材
    public function addInventory()
    {

        echo  $this->fetch();
    }

    //更新订单状态
    public function edit_status()
    {
        $post_data = Request::post();
        $map['id'] = ['eq', $post_data['order_id']];
        $res = Db::name('order')->where($map)->update(['status' => $post_data['status']]);
        if($res){
            return $this->ajaxSuccess();
        }else{
            return $this->ajaxFail();
        }
    }
    //编辑
    public function edit()
    {
        return $this->fetch();
    }
    //查看
    public function view()
    {
        $id = Request::get('id');
        $map['id'] = ['eq', $id];
        $order_data = Db::name('order')->where($map)->find();
        if(!empty($order_data)){
            $order_goods_data = Db::name('order_goods')->where(['order_id'=>['eq', $order_data['id']]])->select();
//            dump($order_data);
//            dump($order_goods_data);
            $this->assign('order_data',$order_data);
            $this->assign('order_goods_list',$order_goods_data);
        }
        echo  $this->fetch();
    }


    //删除
    public function del()
    {
        $id  = Request::param('id');
        $order_table = Db::name('order');
//        $inventory_table = Db::name('inventory');
        //删除订单恢复库存
        $order_goods_table = Db::name('order_goods');
        //得到订单中的耗材
        $get_order_goods = $order_goods_table->field(['inventory_id as id','buy_total as surplus_num'])->where(['order_id'=>['eq', $id]])->select();
        if(!empty($get_order_goods)){
            $update_res = $del_res = false;
            //删除订单恢复库存
            //删除订单关联的耗材信息
            foreach ($get_order_goods as $index => $get_order_good){
                $update_res = Db::name('inventory')->where(['id' => ['eq', $get_order_good['id']]])->setInc('surplus_num', $get_order_good['surplus_num']);
            }
            if($update_res) $del_res = $order_goods_table->where(['order_id'=>['eq', $id]])->delete();
            if(!$del_res)    return $this->ajaxFail('-1', '删除订单关联的耗材信息失败');
        }
        //todo 删除记录
         $res = $order_table->delete($id);
        if($res){
            return $this->ajaxSuccess();
        }else{
            return $this->ajaxFail();
        }
    }
}